package cat.sun.graphics;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import cat.sun.logic.Pole;
import cat.sun.themes.ThemePackage;

public class FieldBitmapMaker
{
	private Bitmap one,two,three,four,five,six,seven,eight,open,close,bomb,flag,cross_flag,red_bomb;
	private int cellW;
	private Paint paint;
	private int width = 0,height = 0;
	
	public FieldBitmapMaker(ThemePackage themePackage,Config config, int height, int width)
	{
		this.one = themePackage.getOne();
		this.two = themePackage.getTwo();
		this.three = themePackage.getThree();
		this.four = themePackage.getFour();
		this.five = themePackage.getFive();
		this.six = themePackage.getSix();
		this.seven = themePackage.getSeven();
		this.eight = themePackage.getEight();
		
		this.bomb = themePackage.getBomb();
		this.red_bomb = themePackage.getRedBomb();
		this.flag = themePackage.getFlag();
		this.cross_flag = themePackage.getCrossFlag();
		this.open = themePackage.getOpenCell();
		this.close = themePackage.getCloseCell();		
		 
		cellW = this.one.getHeight();
		paint = new Paint();
		
		this.height = height*cellW+1;
		this.width = width*cellW+1;
	}	
	
	synchronized public int getCellNumber(float x) 
	{
		return (int) (x / cellW);
	}
	
	synchronized public int getPixelNumber(int x) 
	{
		return (x * cellW);
	}
	
	public void EndOfWorking()
	{
		if (this.bomb != null) this.bomb.recycle();
		if (this.red_bomb != null) this.red_bomb.recycle();
		if (this.cross_flag != null) this.cross_flag.recycle();		
		if (this.open != null) this.open.recycle();
		if (this.close != null) this.close.recycle();
		if (this.flag != null) this.flag.recycle();
		if (this.one != null) this.one.recycle();
		if (this.two != null) this.two.recycle();		
		if (this.three != null) this.three.recycle();
		if (this.four != null) this.four.recycle();
		if (this.five != null) this.five.recycle();
		if (this.six != null) this.six.recycle();
		if (this.seven != null) this.seven.recycle();
		if (this.eight != null) this.eight.recycle();		
	}

	public int getFieldWidth() 
	{
		return this.width;
	}

	public int getFieldHeight() 
	{
		return this.height;
	}

	public void addFieldOnCanvas(Canvas canvas, Pole field,int left, int top) 
	{		
		for (int i = 0;i<field.getHeight();i++)
			for (int j=0;j<field.getWidth();j++)
			{
				switch (field.getCell(j, i).getState())
				{
					case CLOSE:canvas.drawBitmap(this.close, left + j*cellW+1,top + i*cellW+1, paint);break;
					case CHECKED:canvas.drawBitmap(this.flag, left +j*cellW+1,top + i*cellW+1, paint);break;
					case CROSS_CHECKED:canvas.drawBitmap(this.cross_flag,left + j*cellW+1, top + i*cellW+1, paint);break;
					case OPEN:
					{
						switch (field.getCell(j, i).getCellContent())
						{
							case BOMB:canvas.drawBitmap(this.bomb,left + j*cellW+1,top + i*cellW+1, paint);break;
							case RED_BOMB:canvas.drawBitmap(this.red_bomb, left +j*cellW+1,top + i*cellW+1, paint);break;
							case NOTHING:canvas.drawBitmap(this.open,left + j*cellW+1,top + i*cellW+1, paint);break;
							case NUMBER:
							{              
								int k = field.getCell(j, i).getNumber();
								switch (k)
								{
									case 1:canvas.drawBitmap(this.one,left + j*cellW+1,top + i*cellW+1, paint);break;
									case 2:canvas.drawBitmap(this.two, left +j*cellW+1,top + i*cellW+1, paint);break;
									case 3:canvas.drawBitmap(this.three,left + j*cellW+1,top + i*cellW+1, paint);break;
									case 4:canvas.drawBitmap(this.four,left + j*cellW+1,top + i*cellW+1, paint);break;
									case 5:canvas.drawBitmap(this.five,left + j*cellW+1,top + i*cellW+1, paint);break;
									case 6:canvas.drawBitmap(this.six,left + j*cellW+1,top + i*cellW+1, paint);break;
									case 7:canvas.drawBitmap(this.seven,left +j*cellW+1,top + i*cellW+1, paint);break;
									case 8:canvas.drawBitmap(this.eight,left + j*cellW+1,top + i*cellW+1, paint);break;
								}
							}
						}
					}
				}				
			}
	}

}
